/*********************************************************************************
 *      Copyright:  (C) 2022 lijiale<244871365@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  bc28_at.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(19/01/22)
 *         Author:  lijiale <244871365@qq.com>
 *      ChangeLog:  1, Release initial version on "19/01/22 11:14:45"
 *                 
 ********************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "comport.h"
#include "atcmd.h"
#include "bc28_at.h"

#define INFORMATION 256
#define TIME_OUT 1500

int atcmd_at(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport )
    {
        printf("open atcmd_at:“at”failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport,"AT\r\n",wbuf,sizeof(wbuf),TIME_OUT);
    if( rv < 0 )
    {
        printf("send_atcmd_AT failure\n");
        return -2;
    }

    rv = at_fetch(wbuf,"\r\n","\r\n",rbuf,sizeof(rbuf));
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    if( strncmp(rbuf,"OK",2)!=0 )
    {
        printf("NBiot is not connection...\n");
        return -4;
    }
    printf("AT:%s\n",rbuf);

    return 0;
}

int atcmd_csq(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if(!comport)
    {
        printf("open atcmd_csq is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport,"AT+CSQ\r\n",wbuf , sizeof(wbuf),TIME_OUT);
    if( rv < 0 )
    {
        printf("send_atcmd_CSQ failure\n");
        return -2;
    }

    rv = at_fetch(wbuf,":",",",rbuf,sizeof(rbuf));
    if( rv < 0 )
    {
        printf("no find signal\n");
        return -3;
    }

    if( atoi(rbuf)<10 || atoi(rbuf)==99 )
    {
        printf("NBiot is not signal...\n");
        return -4;
    }
    printf("AT+CSQ:%s\n",rbuf);
    
    return 0;
}

int atcmd_cgatt1(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport )
    {
        printf("open atcmd_cgatt is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport,"AT+CGATT=1\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGATT=1 is  failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    if(strncmp(rbuf,"OK",2)!=0)
    {
        printf("NBiot is not connection...\n");
        return -4;
    }

    printf( "AT+CGATT?:%s\n", rbuf );

    return 0;
}

int atcmd_cereg(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport )
    {
        printf("open atcmd_cereg is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport,"AT+CEREG?\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CEREG? failure\n");
        return -3;
    }
    
    rv = at_fetch(wbuf, ":", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find 0,1\n");
        return -5;
    }

    if(strncmp(rbuf, "0,1", 3) != 0 )
    {
        printf("NBiot is not register...\n");
        return -4;
    }

    printf("AT+CEREG:%s\n", rbuf);

    return 0;
}

int atcmd_cgatt(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;
    
    if( !comport )
    {
        printf("open atcmd_cgatt1 failure...\n");
        return -1;
    }

    memset(wbuf, 0, sizeof(wbuf) );
    memset(rbuf, 0, sizeof(rbuf) );

    rv = send_atcmd(comport,"AT+CGATT?\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGATT? failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, ":", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find AT+CGATT \n");
        return -3;
    }

    if( atoi(rbuf) != 1 )
    {
        printf("NBiot is not Network attachment...\n");
        return -4;
    }

    printf("AT+CGATT:%s\n",rbuf);

    return 0;
}

//查IMEI号
int atcmd_cgsn(comport_t *comport,char *imei,int size)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport || !imei || size <= 0 )
    {
        printf("open atcmd_cgsn is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport,"AT+CGSN=1\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGSN=1 failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "+", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find AT+CGSN=1 \n");
        return -3;
    }

    strncpy(imei,rbuf,sizeof(rbuf));

    printf("AT+CGSN:%s\n",rbuf);

    return 0;
}

//查询IMSI号码
int atcmd_cimi(comport_t *comport , char *cimi , int size)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport || !cimi || size <= 0 )
    {
        printf("open atcmd_CIMI failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport, "AT+CIMI\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CIMI failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, INFORMATION );
    if( rv < 0 )
    {
        printf("no find AT+CGSN=1 \n");
        return -3;
    }

    strncpy(cimi, rbuf, sizeof(rbuf) );

    printf("AT+CIMI:%s\n",rbuf);

    return 0;
}

//查模块名
int atcmd_cgmm(comport_t *comport ,char *name , int size)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport || !name || size <= 0 )
    {
        printf("open atcmd_cgmm is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport, "AT+CGMM\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGMM failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, INFORMATION );
    if( rv < 0 )
    {
        printf("no find AT+CGMM \n");
        return -3;
    }

    strncpy(name, rbuf,sizeof(rbuf) );

    printf("name :%s\n",name);

    return 0;
}

int atcmd_cgmr(comport_t *comport ,char *version , int size)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if(!comport || !version ||  size <= 0)
    {
        printf("open atcmd_cgmr is failure...\n");
        return -1;
    }

    rv = send_atcmd(comport, "AT+CGMR\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGMR failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "OK", rbuf,sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find AT+CGMr \n");
        return -3;
    }

    strncpy(version, rbuf, sizeof(rbuf) );

    printf("version :%s\n",version);

    return 0;
}

//查询模块IP地址
int atcmd_cgpaddr(comport_t *comport)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if( !comport )
    {
        printf("open atcmd_cgpaddr is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport, "AT+CGPADDR\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+CGPADDR failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, ":1\r\n", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }
    
    printf("%s\n",rbuf);

    if(strncmp(rbuf,"OK", 2)!=0)
    {
        printf("NBiot is not ip...\n");
        return -4;
    }

    printf("AT+CGPADDR:%s\n",rbuf);

    return 0;
}

//设置iot平台IP地址以及端口
int atcmd_ncdp(comport_t *comport,char *ip,char *port)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    char                buf[INFORMATION];
    int                 rv;

    if( !comport )
    {
        printf("open atcmd_ncdp is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));
    memset(buf , 0 , sizeof(buf));

    snprintf(buf, sizeof(buf),"AT+NCDP=%s,%s\r\n",ip,port);

    rv = send_atcmd(comport, buf, wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+NCDP failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    if(strncmp(rbuf, "OK" ,2 ) !=0 )
    {
        printf("NBiot port is failure...\n");
        return -4;
    }

    printf("AT+NCDP:%s\n",rbuf);

    return 0;
}

//设置AT回显
int atcmd_set_echo(comport_t *comport,int flag)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;
    char    str[INFORMATION];

    if( !comport )
    {
        printf("open atcmd_ech* is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    snprintf(str, sizeof(str), "ATE%d\r\n", flag);

    rv = send_atcmd(comport, str, wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_ech%d failure\n",flag);
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find ATE0 \n");
        return -3;
    }

    printf("ATE0:%s\n",rbuf);

    return 0;
}

//上报数据
int atcmd_qlwuladtaex(comport_t *comport,int  temp)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    char                sbuf[INFORMATION];
    char                buf[INFORMATION];
    int                 rv;
    int                 reg;

    if( !comport )
    {
        printf("open atcmd_ok:“OK”failure...\n");
        return -1;
    }

    memset(sbuf , 0 , sizeof(sbuf));
    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    sprintf(buf, "%x", temp);
    printf("at_hwinfo buf:%s\n", buf);

    sprintf(sbuf, "AT+QLWULDATAEX=6,0200040001%s,0x0100\r\n", buf);
    printf("at_hwinfo sbuf:%s\n", sbuf);

    rv = send_atcmd(comport, sbuf, wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT+QLWULDATAEX is  failure rc :%d\n" , rv);
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, 128);
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    if(!strcmp(rbuf, "OK") && !strcmp(rbuf, "+QLWULDATASTATUS") )
    {
        printf("NBiot port is failure... rbuf:%s\n",rbuf);
        return -4;
    }

    printf("AT QLWULDATAEX :%s\n",rbuf);

    return 0;
}

//获取模块信息
int atcmd_ati(comport_t *comport, nbiot_hwinfo_t * ctx)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    int                 rv;

    if(!comport)
    {
        printf("open atcmd_ati is failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    rv = send_atcmd(comport, "ATI\r\n", wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd_AT failure\n");
        return -2;
    }

    rv = at_fetch(wbuf, "\r\n", "\r\n", rbuf, sizeof(rbuf) );
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    strncpy(ctx->manufacturer,rbuf , sizeof(rbuf));  
    printf("ctx->manufacturer :%s\n",ctx->manufacturer);

    strncat(rbuf,"\r\n" , sizeof("\r\n"));

    rv = at_fetch(wbuf,":","\r\n",rbuf,sizeof(rbuf));
    if( rv < 0 )
    {
        printf("no find OK\n");
        return -3;
    }

    strncpy(ctx->num,rbuf , sizeof(rbuf));
    printf("ctx->num :%s\n",ctx->num);

    return 0;
}

//开关射频
int atcmd_cfun(comport_t *comport ,int swtch)
{
    char                wbuf[INFORMATION];
    char                rbuf[INFORMATION];
    char                buf[INFORMATION];
    int                 rv;

    if(!comport)
    {
        printf("open atcmd_at:“at”failure...\n");
        return -1;
    }

    memset(wbuf , 0 , sizeof(wbuf));
    memset(rbuf , 0 , sizeof(rbuf));

    snprintf(buf,64,"AT+CFUN=%d\r\n", swtch);
    printf("806 at+cfun buf:%s \n", buf);

    rv = send_atcmd( comport, buf,wbuf, sizeof(wbuf), TIME_OUT );
    if( rv < 0 )
    {
        printf("send_atcmd %s  failure rc :%d\n",buf,rv);
        return -2;
    }

    printf("at+cfun wbuf:%s\n", wbuf);
    rv = at_fetch(wbuf,"\r\n","\r\n", rbuf, sizeof(rbuf));
    if( rv < 0 )
    {
        printf("at_fetch is failure...\n");
        return -3;
    }

    printf("AT+CFUN=:%s\n", rbuf);

    return 0;
}
